package cs2340.quarterblack.wheresmystuff;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;

public class Privelages extends Activity {
	RadioGroup typeToggle;
	String userId;
	Item item;
	Button btnAccept;
	Button btnCancel;
	Account account;
	
	/**
	 * creates the privileges page
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		account = Model.getAccount();
		setContentView(R.layout.activity_privelages);
		setupActionBar();
		userId = this.getIntent().getStringExtra("userId");
		typeToggle = (RadioGroup)findViewById(R.id.radioGroupPrivelages);
		btnAccept = (Button)findViewById(R.id.priv_accept);
		btnCancel = (Button)findViewById(R.id.priv_cancel);
		
		/**
		 * listens for the accept button press, changes the user's privileges, then 
		 * returns to the previous page
		 */
		btnAccept.setOnClickListener(new OnClickListener(){
			public void onClick(View v)
			{
				if(typeToggle.getCheckedRadioButtonId() == R.id.ci_adminradio)
				{
					account.changePrivelages(userId,true);
					onBackPressed();
				}
				else
				{
					account.changePrivelages(userId, false);
					onBackPressed();
				}
			}
		});
		/**
		 * listens for the cancel button press, returns to previous page
		 */
		btnCancel.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		
	}
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	/**
	 * inflates the options menu
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.just_logout, menu);
		return true;
	}

	/**
	 * specifies the actions associated with each item in the options menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
	case R.id.action_logout:
		Intent homepage = new Intent(Privelages.this,LoginPage.class);
		homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Privelages.this.startActivity(homepage);
		return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
}
